package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    @GetMapping("/dish/page")
    public ResultInfo findByPageAndName(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        String name){
        System.out.println(pageNum);
        System.out.println(pageSize);
        System.out.println(name);
        Page<Dish> pageResult = dishService.findByPageAndName(pageNum,pageSize,name);
        return ResultInfo.success(pageResult);
    }

    @PostMapping("dish")
    public ResultInfo add(@RequestBody Dish dish){
        dishService.add(dish);
        return ResultInfo.success(dish);
    }


    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }

    @PutMapping("dish")
    public ResultInfo update(@RequestBody Dish dish){
        dishService.update(dish);
        return ResultInfo.success(dish);
    }

    @PostMapping("/dish/status/{status}")
    public ResultInfo statusConvert(@PathVariable Integer status,@RequestParam List<Long> ids){
        System.out.println(status);
        System.out.println(ids);
        dishService.statusConvert(status, ids);
        return ResultInfo.success(null);
    }

    @DeleteMapping("/dish")
    public ResultInfo delete(@RequestParam List<Long> ids){
        Boolean flag = dishService.delete(ids);
        return ResultInfo.success(null);
    }

    @GetMapping("/dish/list")
    public ResultInfo findByDish(Long categoryId,String name){
        List<Dish> dishList = new  ArrayList<Dish>();
        if (name == null){
            dishList =  dishService.findByDish(categoryId);
        }else if(categoryId == null){
            dishList =  dishService.findByName(name);
        }
       return ResultInfo.success(dishList);
    }
//
//    @GetMapping("/dish/list")
//    public ResultInfo findByname(String name){
//        List<Dish> dishList =  dishService.findByName(name);
//        return ResultInfo.success(dishList);
//    }
}
